# coding=utf-8
import pymysql.cursors
import log

config = {
    'host': '59.110.137.94',
    'port': 3306,
    'user': 'root',
    'password': '69213535',
    'db': 'secondhandcar',
    'charset': 'utf8',
    'cursorclass': pymysql.cursors.DictCursor,
}


def mysqlinsert(sql):
    db = pymysql.connect(**config)

    # 使用cursor()方法获取操作游标
    cursor = db.cursor()

    # 使用execute方法执行SQL语句
    try:
        log.info(sql)
        # 执行sql语句
        with db.cursor() as cursor:
            cursor.execute(sql)
            # 提交到数据库执行
            db.commit()
    except:
        # Rollback in case there is any error
        log.error("插入失败，已经有该条数据")
        db.rollback()

    # 关闭数据库连接
    db.close()
